import pandas as pd
import requests
import json
import os
from tqdm import tqdm  # 进度条工具
import time

# 读取CSV文件
df = pd.read_csv('GZsid_with_links.csv', encoding='utf-8-sig')

# 创建存储JSON的目录
os.makedirs('jsons_GZ',exist_ok=True)

# 设置请求头模拟浏览器
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}

# 处理每条记录
for index, row in tqdm(df.iterrows(), total=len(df), desc="下载JSON进度"):
    try:
        # 1. 下载JSON数据
        response = requests.get(row['下载链接'], headers=headers, timeout=10)
        response.raise_for_status()  # 检查请求是否成功
        
        data = response.json()
        
        # 2. 为每条记录添加分类信息
        # 从层级说明中提取分类信息（例如："3D模型分类（新版）,家装,灯具" -> 取前两部分）
        hierarchy_parts = row['层级说明'].split(',')
        parent_category = ','.join(hierarchy_parts[1:])  # 取前两部分作为父级分类
        
        if isinstance(data, list):
            for item in data:
                item['父级分类名称'] = parent_category
                item['分类名称'] = row['分类名称']
        elif isinstance(data, dict):
            data['父级分类名称'] = parent_category
            data['分类名称'] = row['分类名称']
        
        # 3. 保存JSON文件
        filename = f"jsons_GZ/{row['id']}.json"
        with open(filename, 'w', encoding='utf-8') as f:
            json.dump(data, f, ensure_ascii=False, indent=2)
        
        # 礼貌性延迟，避免被封
        time.sleep(0.5)
        
    except Exception as e:
        print(f"\n处理ID {row['id']} 时出错: {str(e)}")
        # 保存错误信息到日志
        with open('download_errors.log', 'a', encoding='utf-8') as f:
            f.write(f"ID:{row['id']}, 错误:{str(e)}\n")

print("\n所有JSON文件已下载并处理完成，保存在 downloaded_jsons 目录")